package com.imonsoft.sqm.web.basedata;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.imonsoft.common.constant.CCommon;
import com.imonsoft.common.constant.ConstantSqm;
import com.imonsoft.common.exception.SystemException;
import com.imonsoft.common.httpModel.DataGrid;
import com.imonsoft.common.httpModel.Pager;
import com.imonsoft.common.httpModel.ReturnMessage;
import com.imonsoft.common.util.AjaxUtil;
import com.imonsoft.common.util.JsonUtil;
import com.imonsoft.sqm.pojo.basedata.IpicTarget;
import com.imonsoft.sqm.pojo.basedata.Supplier;
import com.imonsoft.sqm.service.basedata.IpicTargetService;
import com.imonsoft.sqm.service.basedata.SupplierService;
import com.imonsoft.sqm.validator.basedata.IpicTargetValidator;

@Controller
@RequestMapping("/basedata/ipic_target")
public class IpicTargetController {
	protected final Log logger = LogFactory.getLog(getClass());
	@Autowired
	private IpicTargetService ipicTargetService;
	@Autowired
	private SupplierService supplierService;
	@Autowired
	private IpicTargetValidator ipicTargetValidator;
	@RequestMapping("/listUI.do")
	public void listUI() {
	}

	@RequestMapping(value="/ajaxList.do", method = RequestMethod.POST)
	public void ajaxList(HttpServletResponse response, Pager pager, IpicTarget ipicTarget) {
		DataGrid<IpicTarget> dataGrid = new DataGrid<IpicTarget>();
		try {
			dataGrid = ipicTargetService.getDataGrid(ipicTarget, pager);
		} catch (SystemException se) {
			logger.debug(se.getMessage());
		} catch (Exception e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		}
		AjaxUtil.renderJSON(response, JsonUtil.bean2json(dataGrid));
	}

	@RequestMapping("/addUI.do")
	public void addUI(ModelMap model) {
		IpicTarget ipicTarget = new IpicTarget();
		model.put("dictionary", ipicTarget);
	}

	@RequestMapping(value = "/ajaxAdd.do", method = RequestMethod.POST)
	public void ajaxAdd(HttpServletResponse response, IpicTarget ipicTarget, BindingResult result) {
		ReturnMessage message = new ReturnMessage();
		try {
			Supplier supplier = supplierService.get(Long.valueOf(ipicTarget.getSupplierId()));
			ipicTarget.setSupplierCode(supplier.getCode());
			ipicTarget.setSupplierName(supplier.getName());
			ipicTargetValidator.methodAddValidate(ipicTarget, result);
			if (result.hasErrors()) {
				message.setObj(ipicTargetValidator.getErrors(result));
			}else{
				ipicTargetService.insert(ipicTarget);
				message.setSuccess(true);
				message.setMsg(ConstantSqm.OPERATE_SUCCESS);
			}
		} catch (SystemException se) {
			logger.debug(this.getClass() + ":" + se.getMessage());
			message.setMsg(se.getMessage());
		} catch (Exception e) {
			logger.error(this.getClass() + ":" + e.getMessage());
			message.setMsg(CCommon.EXCEPTION_MSG);
		}
		AjaxUtil.renderJSON(response, JsonUtil.bean2json(message));
	}

	@RequestMapping(value = "/modifyUI.do")
	public void modifyUI(ModelMap model, Long id) {
		if (null != id) {
			IpicTarget ipicTarget = ipicTargetService.get(id);
			model.put("ipicTarget", ipicTarget);
		} else {
			new Exception(CCommon.EXCEPTION_MSG);
		}
	}

	@RequestMapping(value = "/ajaxModify.do", method = RequestMethod.POST)
	public void ajaxModify(HttpServletResponse response, IpicTarget ipicTarget, BindingResult result) {
		ReturnMessage message = new ReturnMessage();

		try {
			ipicTargetService.update(ipicTarget);
			message.setSuccess(true);
			message.setMsg(ConstantSqm.OPERATE_SUCCESS);

		} catch (SystemException se) {
			logger.debug(this.getClass() + ":" + se.getMessage());
			message.setMsg(se.getMessage());
		} catch (Exception e) {
			logger.error(this.getClass() + ":" + e.getMessage());
			message.setMsg(CCommon.EXCEPTION_MSG);
		}
		AjaxUtil.renderJSON(response, JsonUtil.bean2json(message));
	}

	@RequestMapping(value = "/ajaxDelete.do", method = RequestMethod.POST)
	public void ajaxDelete(HttpServletResponse response,@RequestParam("ids") List<Long> ids) {
		ReturnMessage message = new ReturnMessage();
		try {
			ipicTargetService.deleteIpicTarget(ids);
			message.setSuccess(true);
			message.setMsg(ConstantSqm.OPERATE_SUCCESS);
		} catch (SystemException se) {
			logger.debug(this.getClass() + ":" + se.getMessage());
			message.setMsg(se.getMessage());
		} catch (Exception e) {
			logger.error(this.getClass() + ":" + e.getMessage());
			message.setMsg(CCommon.EXCEPTION_MSG);
		}
		AjaxUtil.renderJSON(response, JsonUtil.bean2json(message));
	}
}
